define(function (require, exports, module) {

    var PDFAnnot = WebPDF.PDFData.PDFAnnot;
    var annotFactory = WebPDF.AnnotFactory;

    WebPDF.Tools.TOOL_NAME_TYPERWRITER = "FreeTextTypewriter";

    WebPDF.Event.TypewriterAnnotToolHandler = function (readerApp, annotType, annotPlugin) {
        var _readerApp = readerApp,
    		_baseAnnotPlugin = annotPlugin,
    		_typewriterAnnotType = annotType,
    		_isProcessing = false,
            _self = this;

        /**
        * @description 氓藛锟矫ヂр�氓艗鈥撁ヂ仿ッモ�路忙鈥斅睹捌捗р�篓
        * @param {WebPDF.CReaderApp} pReaderApp App氓炉鹿猫卤隆
        */
        this.onInit = function () {
            
        };

        this.onDestroy = function () {
        };

        /**
        * @description 猫沤路氓锟解�氓路楼氓鈥β访♀�氓锟斤拷莽搂掳
        * @return {String} 氓路楼氓鈥β访♀�氓锟斤拷莽搂掳;
        */
        this.getName = function () {
            return _typewriterAnnotType;
        };

        // UI Events
        /**
        * @description 忙驴鈧β绰幻ヂ仿ッモ�路忙鈥斅睹捌捗р�篓
        */
        this.onActivate = function () {
            var docView = _readerApp.getMainView().getDocView();
            var jqDocViewContainer = $("#" + docView.getDocViewContainerID());
            if (_typewriterAnnotType === WebPDF.PDFData.TypewriterAnnotType.TYPEWRITERTYPE_TYPEWRITER) {
                if (WebPDF.Environment.ie || WebPDF.Environment.ieAtLeast11)
                    jqDocViewContainer.addClass("fwr-typewriter-cursor-ie");
                else
                    jqDocViewContainer.addClass("fwr-typewriter-cursor");
            }
        };


        /**
        * @description 氓锟脚撁р�篓氓路楼氓鈥β访︹�露猫掳茠莽鈥澛�
        */
        this.onDeactivate = function () {
            var docView = _readerApp.getMainView().getDocView();
            var jqDocViewContainer = $("#" + docView.getDocViewContainerID());
            if (_typewriterAnnotType === WebPDF.PDFData.TypewriterAnnotType.TYPEWRITERTYPE_TYPEWRITER) {
                if (WebPDF.Environment.ie || WebPDF.Environment.ieAtLeast11)
                    jqDocViewContainer.removeClass("fwr-typewriter-cursor-ie");
                else
                    jqDocViewContainer.removeClass("fwr-typewriter-cursor");
            }
        };

        // Calculate status
        /**
        * @description 氓藛陇忙鈥撀ッヂ仿ッモ�路忙藴炉氓锟铰γワ拷炉莽鈥澛�
        * @return {Boolean} 氓锟铰р�篓猫驴鈥澝モ�啪true,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.isEnabled = function () {
            var currentDocument = _readerApp.getMainView().getDocView().getPDFDoc();
            if (currentDocument) {
                if (currentDocument.getDocType() != WebPDF.PDFDocType.XFA)
                    return true;
            }
            /*
            * if (!(pDoc->GetDocumentType() == DOCTYPE_DYNIMIC_XFA))
            return pDoc->GetPermissions(FPDFPERM_ANNOT_FORM) ; 
            * */
            return true;
        };

        /**
        * @description 氓藛陇忙鈥撀ッヂ仿ッモ�路忙藴炉氓锟铰γβＣヂも�盲潞沤忙鈥撯�忙艙卢茅鈧�忙鈥孤┟犅睹︹偓锟�
        * @return {Boolean} 忙颅拢氓陇鈥灻ぢ号矫︹�鈥∶ε撀┾偓鈥懊︹�漏莽艩露忙鈧拷猫驴鈥澝モ�啪true,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.isProcessing = function () {
            return _isProcessing;
        };

        // Mouse events
        /**
        * @description 茅录聽忙聽鈥∶ヂ仿γ┾�庐忙艗鈥懊ぢ糕�盲潞鈥姑ぢ宦睹ヂも�莽锟解�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onLButtonDown = function (event) {
            var docView = _readerApp.getMainView().getDocView();
            var jqDocViewContainer = $("#" + docView.getDocViewContainerID());
            if (!WebPDF.Common.containsNode(jqDocViewContainer.get(0), event.target, true)) {
                return false;
            }

            //褰撳墠榧犳爣鏄惁鍦ㄩ〉闈腑
            var pageCount = docView.getPageCount();
            var pageView = null;
            var pageViewRect = null;
            for (var i = 0; i < pageCount; i++) {
                var tmpPageView = docView.getPageView(i);
                var pageOffsetHtmlDoc = $("#" + tmpPageView.getPageViewContainerID()).offset();
                var pageViewWidth = tmpPageView.getPageViewWidth();
                var pageViewHeight = tmpPageView.getPageViewHeight();
                pageViewRect = new WebPDF.PDFRect(pageOffsetHtmlDoc.left, pageOffsetHtmlDoc.top,
						pageOffsetHtmlDoc.left + pageViewWidth, pageOffsetHtmlDoc.top + pageViewHeight);
                WebPDF.RectUtils.normalize(pageViewRect);
                if (WebPDF.RectUtils.ptInRect(pageViewRect, event.pageX, event.pageY)) {
                    pageView = tmpPageView;
                    break;
                }
            }

            if (pageView != null) {
                _self.createAnnot(pageView, event.pageX - pageViewRect.left, event.pageY - pageViewRect.bottom);
                docView.setModified(true);
//                _baseAnnotPlugin.setSaveCommentBtnState(true);
            }

            _isProcessing = true;
            return true;
        };

        /**
        * @description 茅录聽忙聽鈥∶ヂ仿γ┾�庐氓录鹿猫碌路盲潞鈥姑ぢ宦睹ヂも�莽锟解�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onLButtonUp = function (event) {
            _isProcessing = false;
            return true;
        };

        this.createAnnot = function (pageView, pageXofPageView, pageYofPageView) {
            var dt = new Date();
            var time = dt.valueOf() + dt.getTimezoneOffset() * 60000;
            var timestamp = (Math.floor(time / 1000)).toString();
            var annots_json = {
                c: "",
                ca: 1.0,
                q: 0,
                cd: timestamp,
                cl: "",
                f: WebPDF.PDFData.AnnotFlag.ANNOTFLAG_PRINT,
                md: timestamp,
                n: timestamp,
                subj: "Typewriter",            //  _Language.Annot.AnnotSubjTypewriter,
                subty: "FreeText",
             //   tit: _pReaderApp.GetUserName(),
                ty: "Typewriter",
                fs: WebPDF.PDFData.TypewriteAnnotDefaultValue.FontSize,
                fh: WebPDF.PDFData.TypewriteAnnotDefaultValue.FontHeight,
                lnh: WebPDF.PDFData.TypewriteAnnotDefaultValue.LineHeight,
                fc: "#0000ff",
                ftn: "Helvetica",
                it: _typewriterAnnotType,
				r: pageView.getPDFPage().getPageRotate()*90,
                rc: null
            };

            var pPDFPage = pageView.getPDFPage();
            var pdfPageWidth = pPDFPage.getPageWidth();
            var pdfPageHeight = pPDFPage.getPageHeight();
            var curPointPDF = new WebPDF.PDFPoint(pageXofPageView, pageYofPageView);
            curPointPDF = pageView.docPtToPDF(curPointPDF);

            var pdfrect = new WebPDF.PDFRect(curPointPDF.x, curPointPDF.y, curPointPDF.x + WebPDF.PDFData.TypewriteAnnotDefaultValue.NewAnnotWidth,
        			 curPointPDF.y - WebPDF.PDFData.TypewriteAnnotDefaultValue.LineHeight);

            if (pdfrect.left < 0) {
                WebPDF.RectUtils.offset(pdfrect, 0 - pdfrect.left, 0);
            }
            if (pdfrect.bottom < 0) {
                WebPDF.RectUtils.offset(pdfrect, 0, 0 - pdfrect.bottom);
            }
            if (pdfrect.right > pdfPageWidth) {
                WebPDF.RectUtils.offset(pdfrect, pdfPageWidth - pdfrect.right, 0);
            }
            if (pdfrect.top > pdfPageHeight) {
                WebPDF.RectUtils.offset(pdfrect, 0, pdfPageHeight - pdfrect.top);
            }

            annots_json.rc = [pdfrect.left, pdfrect.top, pdfrect.right, pdfrect.bottom];

            var pAnnot = WebPDF.AnnotFactory.createAnnot(annots_json, false);
            pPDFPage.getAnnotJsonData().push(annots_json);
            pPDFPage.addAnnot(pAnnot);
            pageView.setModified(true);
            var annotHandlerMgr = _readerApp.getPluginByName(WebPDF.BASEANNOT_PLUGIN_NAME).getAnnotHandlerMgr();
            var pageAnnotsDiv = annotHandlerMgr.getUIManager().getPageAnnotsContainerID(pageView);
            var jqPageAnnotsDiv = $("#" + pageAnnotsDiv);
            var annotName = pAnnot.getAnnotName();
            var newAnnotHtml = annotHandlerMgr.getUIManager().createAAnnotHtmlContent(pageView, pAnnot, annotName);
            jqPageAnnotsDiv.append(newAnnotHtml);

            var annotHandler = annotHandlerMgr.getAnnotHandler(WebPDF.PDFData.TypewriterAnnotType.TYPEWRITERTYPE_TYPEWRITER);
            annotHandlerMgr.setFocusAnnot(pageView, pAnnot);
            annotHandlerMgr.selectAnnot(pageView, pAnnot);
            annotHandler.onTypewriterAnnotEdit(pageView,pAnnot);
        };



        /**
        * @description 茅录聽忙聽鈥∶ヂ仿γ┾�庐氓锟脚捗モ�禄盲潞鈥姑ぢ宦睹ヂも�莽锟解�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onLButtonDblClk = function (event) {
            return false;
        };
        /**
        * @description 盲赂禄莽陋鈥斆ぢ解�莽拧鈥灻┞悸犆β犫�莽搂禄氓艩篓盲潞鈥姑ぢ宦睹ヂも�莽锟解�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseMove = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶ワ拷鲁茅鈥澛ε掆�盲赂鈥姑ぢ衡�盲禄露氓陇鈥灻э拷鈥�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onRButtonDown = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶ワ拷鲁茅鈥澛ヂ悸姑德访ぢ衡�盲禄露氓陇鈥灻э拷鈥�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onRButtonUp = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶ワ拷鲁茅鈥澛ワ拷艗氓鈥÷幻ぢ衡�盲禄露氓陇鈥灻э拷鈥�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onRButtonDblClk = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶β慌∶铰β慌∶ヅ犅ぢ衡�盲禄露氓陇鈥灻э拷鈥�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseWheel = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶幻库�盲潞鈥姑ぢ宦睹妓喢┞悸犆β犫�莽搂禄氓艩篓氓藛掳氓庐茠氓颅锟矫モ�茠莽麓聽忙鈥斅睹ぢ古该ぢ寂∶γワ拷鈥樏ッぢ衡�盲禄露茂录鈥懊ヂも�莽锟解�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseOver = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶幻モ�潞盲潞鈥姑ぢ宦睹妓喢┞悸犆β犫�莽搂禄氓艩篓氓藛掳氓庐茠氓颅锟矫モ�茠莽麓聽忙鈥斅睹ぢ古该ぢ寂∶γワ拷鈥樏ッぢ衡�盲禄露茂录鈥懊ヂも�莽锟解�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseOut = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶库�氓鈥βッぢ衡�盲禄露茂录藛茅录聽忙聽鈥∶ワ拷陋忙艙鈥懊库�氓鈥βッッモ�茠莽麓聽氓艗潞氓鸥鸥忙鈥帮拷盲录拧猫搂娄氓锟解�猫炉楼盲潞鈥姑ぢ宦睹尖�氓陇鈥灻э拷鈥�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseEnter = function (event) {
            return false;
        };
        /**
        * @description 茅录聽忙聽鈥∶β幻ヂ尖偓盲潞鈥姑ぢ宦睹妓喢┞悸犆β犫�氓锟铰ε撯�莽娄禄氓录鈧ッモ�茠莽麓聽氓艗潞氓鸥鸥忙鈥帮拷盲录拧猫搂娄氓锟解�猫炉楼盲潞鈥姑ぢ宦睹尖�氓陇鈥灻э拷鈥�
        * @param {Object} event 茅录聽忙聽鈥∶ぢ衡�盲禄露氓炉鹿猫卤隆
        * @return {Boolean} 氓陇鈥灻э拷鈥犆库�氓鈥号総rue,氓锟铰γニ嗏劉猫驴鈥澝モ�啪false;
        */
        this.onMouseLeave = function (event) {
            return false;
        };
        this.onKeyDown = function (event) {
            return false;
        };
        this.onKeyUp = function (event) {
            return false;
        };
        this.onChar = function (event) {
            return false;
        };
    };

    return WebPDF.Event.TypewriterAnnotToolHandler;
});